﻿#region  Copyright (C) 2013 Unisys Corporation

// =============================================================================================
// All Rights Reserved. This work contains trade secrets and confidential material of UNISYS, 
// and its use or disclosure in whole or in part without explicit written permission of 
// UNISYS is prohibited. 
//  
// Product Name          :  Unisys e-Workflow and Imaging
// File Name             :  DuplicateService.cs
// Authored By           :  Unisys Corporation
// =============================================================================================
//	Modified by                         Date            Version
// =============================================================================================
//	ronald.aguilar@cr.unisys.com      May 01, 2013    Initial Version
// =============================================================================================
#endregion

using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Entidades.Helper;
namespace Entidades.Model
{

    /// <summary>
    /// DuplicateService parent class for the XML serialization.
    /// </summary>
    /// 
    /*
    [Serializable]
    [XmlRoot(ElementName = "DuplicateService", IsNullable = false)]
    public class DuplicateService
    {
        [XmlElement("Duplicate")]
        public Duplicate Duplicates { get; set; }
        [XmlElement("Exception")]
        public ExceptionDocClass Exceptions { get; set; }
        [XmlElement("Inconsistence")]
        public Incosistence Inconsistences { get; set; }
    }

    /// <summary>
    /// Duplicate list parent node.
    /// </summary>
    [Serializable]
    public class Duplicate
    {
        [XmlElement("DocClass")]
        public List<DocClass> DocClasses { get; set; }
    }

    /// <summary>
    /// Duplicate document description node.
    /// </summary>
    [Serializable]
    public class DocClass
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("NewName")]
        public string NewName { get; set; }
        [XmlAttribute("DateFieldName")]
        public string DateFieldName { get; set; }
        [XmlAttribute("DateFieldFormat")]
        public string DateFieldFormat { get; set; }
        [XmlElement("Field")]
        public List<string> Fields { get; set; }
        [XmlElement("Action")]
        public DocAction DocAction { get; set; }
        [XmlAttribute("ForceOutputWip")]
        public bool ForceOutputWip { get; set; }
    }

    /// <summary>
    /// ExceptionDocClass parent node.
    /// </summary>
    [Serializable]
    public class ExceptionDocClass
    {
        [XmlElement("DocClassExc")]
        public List<DocClassExc> DocClassesExc { get; set; }
    }

    /// <summary>
    /// ExceptionDocClass document description node.
    /// </summary>
    [Serializable]
    public class DocClassExc
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("NewName")]
        public string NewName { get; set; }
        [XmlElement("FieldExc")]
        public List<FieldExc> FieldsExc { get; set; }
        [XmlElement("Action")]
        public DocAction DocAction { get; set; }
        [XmlAttribute("ForceOutputWip")]
        public bool ForceOutputWip { get; set; }
    }

    /// <summary>
    /// Action over duplicate items node.
    /// </summary>
    [Serializable]
    public class DocAction
    {
        [XmlAttribute("DestinationQueue")]
        public string DestinationQueue { get; set; }
        [XmlElement("FieldAction")]
        public List<FieldAction> FieldActions { get; set; }
    }

    /// <summary>
    /// Field to filter action over duplicates, child node.
    /// </summary>
    [Serializable]
    public class FieldAction
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    /// <summary>
    /// Represent a field condition to be applied to ExceptionDocClass.
    /// </summary>
    [Serializable]
    public class FieldExc
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlElement("Value")]
        public List<string> Values { get; set; }
    }

    /// <summary>
    /// Represent a field condition to be applied to InconsistenceClass.
    /// </summary>
    [Serializable]
    public class Incosistence
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlAttribute("NewName")]
        public string NewName { get; set; }
        [XmlElement("DocAction")]
        public List<DocAction> DocActions { get; set; }
        [XmlElement("FieldAction")]
        public List<FieldAction> Fields { get; set; }

    }*/


    [Serializable]
    [XmlRoot(ElementName = "DuplicateService", IsNullable = false)]
    public class DuplicateService
    {

        [XmlElement("DocClass")]
        public List<DocClass> DocClassList { get; set; }


        [XmlElement("DocClassExc")]
        public List<DocClassExc> DocClassExcList { get; set; }

        [XmlElement("Incosistencies")]
        public List<Incosistence> IncosistenciesList { get; set; }

        [XmlElement("Rejected")]
        public List<Rejected> rejectedClass { get; set; }

        #region Methods

        public static DuplicateService loadBusinessRules()
        {
            DuplicateService duplicate = new DuplicateService();
            duplicate = Deserializer.Deserialize<DuplicateService>(Configuration.duplicateRules);
            return duplicate;

        }

        #region GetData
        public Incosistence getIncosistence(string className)
        {
            foreach (Incosistence incon in IncosistenciesList)
            {
                if (incon.Name.Equals(className))
                    return incon;
            }
            return null;
        }

        public DocClassExc getException(string className,string unitName)
        {
            foreach (DocClassExc exc in DocClassExcList)
            {
                if (exc.ClassName.Equals(className))
                    if(exc.UnitName.Equals(unitName))
                        return exc;
            }
            return null;
        }

        public DocClass getDuplicateClass(string className,string unitName)
        {
            foreach (DocClass docClass in DocClassList)
            {
                if ((docClass.Name.Equals(className)) && (docClass.unitName.Equals(unitName)))
                    return docClass;
            }
            return null;
        }

        public Rejected getRejectedClass(string className)
        {
            foreach (Rejected rejected in rejectedClass)
            {
                if (rejected.Name.Equals(className))
                    return rejected;
            }
            return null;
        }

        #endregion
        
        #region Strings for Queries
        public string getAllClasses()
        {
           string result = ""; 
            foreach (DocClass docClassElement in DocClassList){
                result += "'" + docClassElement.Name + "',";
            }
            foreach (DocClassExc DocClassExcElement in DocClassExcList)
            {
                result += "'" + DocClassExcElement.ClassName + "',";
            }
            foreach (Incosistence IncosistenceElement in IncosistenciesList)
            {
                result += "'" + IncosistenceElement.Name + "',";
            }
            foreach (Rejected reject in rejectedClass)
            {
                result += "'" + reject.Name + "',";
            }
            return result.Remove(result.Length-1);
        }        
      
        #endregion

        #endregion

    }

    [Serializable]
    public class DocClass
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("NewName")]
        public string NewName { get; set; }

        [XmlAttribute("UnitName")]
        public string unitName { get; set; }

        [XmlAttribute("DateFieldName")]
        public string DateFieldName { get; set; }
        

        [XmlAttribute("outputQueue")]
        public string outputQueue { get; set; }


        [XmlAttribute("DocAction")]
        public string DocActions { get; set; }

        [XmlElement("Field")]
        public List<IndexField> Fields { get; set; }
       
    }


    [Serializable]
    public class DocClassExc
    {
        [XmlAttribute("ClassName")]
        public string ClassName { get; set; }

        [XmlAttribute("UnitName")]
        public string UnitName { get; set; }

        [XmlAttribute("NewClassName")]
        public string NewName { get; set; }

        [XmlAttribute("outputQueue")]
        public string outputQueue { get; set; }


        [XmlAttribute("DocAction")]
        public string DocActions { get; set; }

    }


    [Serializable]
    public class Incosistence
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }        

        [XmlAttribute("NewName")]
        public string NewName { get; set; }

        [XmlAttribute("outputQueue")]
        public string outputQueue { get; set; }

        [XmlAttribute("DocAction")]
        public string DocActions { get; set; }

        [XmlElement("Field")]
        public List<IndexField> Fields { get; set; }

    }

    public class duplicateDocument
    {
        public List<IndexField> Fields { get; set; }

        public string getVENCEValue()
        {
            foreach (IndexField field in Fields)
            {
                if (field.Name.Equals("VENCE"))//encuentra el campo vence y devuelve su valor
                    return field.Value;
            }
            return "";
        }
        public string getIDValue()
        {
            foreach (IndexField field in Fields)
            {
                if ((field.Name.Equals("ID")) || (field.Name.Equals("OBJECT_ID")))//encuentra el campo ID y devuelve su valor
                    return field.Value;
            }
            return "";
        }

        public string getValue(string indexName)
        {
            foreach (IndexField field in Fields)
            {
                if (field.Name.Equals(indexName))//encuentra el campo y devuelve su valor
                    return field.Value;
            }
            return "";
        }

    }

    [Serializable]
    public class IndexField
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }
        [XmlText]
        public string Value { get; set; }
    }

    [Serializable]
    public class Rejected
    {

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("NewName")]
        public string NewName { get; set; }

        [XmlAttribute("FieldName")]
        public string FieldName { get; set; }

        [XmlAttribute("FieldValue")]
        public string FieldValue { get; set; }

        [XmlAttribute("outputQueue")]
        public string outputQueue { get; set; }


    } 
}

